#include "ble_receive_task.h"
#include "ble_send_task.h"

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include "ohos_init.h"
#include "cmsis_os2.h"

#include "uart.h"
#include "oled.h"

#include "device_app.h"

uint8_t receive_buff[32] = {0};

ble_receive_data_t ble_receive_data;
extern ble_send_data_t ble_send_data;
extern terminal_send_data_t terminal_send_data;

static void ble_receive_data_init(void);

static void ble_reset(void);
static void ble_base(void);
static void ble_access(void);

static void ble_user(void);
static void ble_get_slave_id(void);
static void ble_user_power_off_respond(void);
static void ble_user_power_on_respond(void);
static void ble_user_relay_state(void);

static void ble_user_timing_off_respond(void);
static void ble_user_timing_on_respond(void);
static void ble_user_time_off_respond(void);
static void ble_user_time_on_respond(void);

void ble_receive_task(void *arg)
{
    (void) arg;
    ble_receive_data_init();
    while(1)
    {
        UartRead(WIFI_IOT_UART_IDX_2, receive_buff, sizeof(receive_buff));
        switch(receive_buff[1])
        {
            case 0x42 : ble_user(); break;    //自定义的数据协议//每秒一个心跳包，没有心跳包的就是离线的
            case 0x40 : ble_base(); break;    //设备入网等基本功能
            case 0x43 : ble_reset(); break;    //恢复出厂设置与重启设备////设备入网错误入口
            default : break;
        }
        bzero(receive_buff, sizeof(receive_buff));
        osDelay(10);   //0.1s
    }
}

void ble_receive_task_creat(void)
{
    osThreadAttr_t attr;

    attr.name       = "ble_receive_task";
    attr.attr_bits  = 0U;
    attr.cb_mem     = NULL;
    attr.cb_size    = 0U;
    attr.stack_mem  = NULL;
    attr.stack_size = 2048;
    attr.priority   = osPriorityNormal2;

    if (osThreadNew(ble_receive_task, NULL, &attr) == NULL)
    {
        printf("[ble_receive_task_creat] Falied to create ble_receive_task!\n");
    }
}

APP_FEATURE_INIT(ble_receive_task_creat);

static void ble_receive_data_init(void)
{
    bzero(receive_buff, sizeof(receive_buff));
    ble_receive_data.ble_num = 0;
    ble_receive_data.ble_num_last = 0;
    ble_receive_data.ble_state = PAIRED;
    for(uint8_t i=0; i<SLAVE_NUM_MAX; i++)
    {
        ble_receive_data.ble_slave_id_data[i].ble_slave_state_last = OFF_LINE;
        ble_receive_data.ble_slave_id_data[i].ble_slave_state = OFF_LINE;
    }
}


static void ble_reset(void)
{
    switch(receive_buff[2])
    {
        case 0x15 : ble_receive_data.ble_state = RESTORED; ble_send_data.ble_send_cmd_num = BLE_CMD_MIN; break;
        case 0x14 : break;//reset ok
        case 0x09 : ble_access(); break;   //设备入网错误入口
        default :  break;    //reset error
    }
}

static void ble_base(void)
{
    switch(receive_buff[2])
    {
        case 0x09 : ble_access(); break;
        default : break;
    }
}

static void ble_access(void)
{
    uint8_t show[32];
    if(receive_buff[0] == 0x0B)
    {
        ble_receive_data.ble_slave_id_data[ble_receive_data.ble_num].ble_slave_id[0] = receive_buff[9];
        ble_receive_data.ble_slave_id_data[ble_receive_data.ble_num].ble_slave_id[1] = receive_buff[10];
        ble_send_data.ble_send_cmd_connect_replay = 1;//主动发送一次入网成功消息给从机
        ble_receive_data.ble_state = CONNECTED;
        ble_receive_data.ble_slave_id_data[ble_receive_data.ble_num].ble_slave_state = ON_LINE;
        ble_receive_data.ble_num++;
    }
    else if(receive_buff[0] != 0x0B)
    {
        switch(receive_buff[3])
        {
            case 0xFC: strcpy((char *)show, "not found"); OLED_Show_String(0, 1, 8, show); break;
            case 0xFB: strcpy((char *)show, "0xFB"); OLED_Show_String(0, 1, 8, show); break;
            case 0xFA: strcpy((char *)show, "netkey failed"); OLED_Show_String(0, 1, 8, show); break;
            case 0xF9: strcpy((char *)show, "appkey timeout"); OLED_Show_String(0, 1, 8, show); break;
            case 0xF8: strcpy((char *)show, "appkey failed"); OLED_Show_String(0, 1, 8, show); break;
            default : strcpy((char *)show, "error"); OLED_Show_String(0, 1, 8, show); break;
        }
        return;
    }
}
/*
0x00请求连接主机
0x01获取主机时间
0x02关电源命令响应
0x03开电源命令响应
0x04继电器状态
*/

static void ble_user(void)
{
    switch(receive_buff[6])
    {
        case 0x00 : ble_get_slave_id(); ble_send_data.ble_send_cmd_connect_replay = 1; break; //主机收到连接请求后，发送一个连接成功的消息给从机
        case 0x01 : ble_get_slave_id(); ble_send_data.ble_send_cmd_time = 1;  break;    //获取时间//ble_send_task发送时间信息
        case 0x02 : ble_get_slave_id(); ble_user_power_off_respond(); break;
        case 0x03 : ble_get_slave_id(); ble_user_power_on_respond(); break;
        case 0x04 : ble_get_slave_id(); ble_user_relay_state(); break;
        case 0x05 : break;
        case 0x06 : ble_get_slave_id(); ble_user_timing_off_respond(); break;
        case 0x07 : ble_get_slave_id(); ble_user_timing_on_respond(); break;
        case 0x08 : ble_get_slave_id(); ble_user_time_off_respond(); break;
        case 0x09 : ble_get_slave_id(); ble_user_time_on_respond(); break;

        default : break;
    }
}

static void ble_get_slave_id(void)
{
    uint8_t i = 0;
    for(i = 0; i<ble_receive_data.ble_num; i++)//比较是否已有相同的从机id，没有就新存入从机id
    {
        if(receive_buff[2] == ble_receive_data.ble_slave_id_data[i].ble_slave_id[0])
        {
            if(receive_buff[3] == ble_receive_data.ble_slave_id_data[i].ble_slave_id[1])
            {
                ble_receive_data.ble_slave_id_data[i].ble_slave_state = ON_LINE;//连接状态更新为正常连接。
                ble_receive_data.ble_slave_id_data[i].ble_slave_time = 0;//清除计时数，开始离线计时
                ble_send_data.slave_id[0] = receive_buff[2];
                ble_send_data.slave_id[1] = receive_buff[3];
                terminal_send_data.send_slave_num = i;
                break;//已有相同的从机
            }
        }
    }
    if(i == ble_receive_data.ble_num)//是新连接成功的从机
    {
        ble_receive_data.ble_slave_id_data[ble_receive_data.ble_num].ble_slave_id[0] = receive_buff[2];//保存新连接的id
        ble_receive_data.ble_slave_id_data[ble_receive_data.ble_num].ble_slave_id[1] = receive_buff[3];
        ble_send_data.slave_id[0] = receive_buff[2];
        ble_send_data.slave_id[1] = receive_buff[3];
        ble_receive_data.ble_num++;//更新正常连接的设备数，如果有设备离线怎么处理该设备？？？，怎么给指定设备发送指令。怎么把设备状态更新给服务器。  
    }
}

static void ble_user_power_off_respond(void)
{
    switch(receive_buff[7])
    {
        case 0x00 :
        {
            ble_send_data.ble_send_user_cmd_state = CMD_SUCCEED;
            if(terminal_send_data.send_power_off_result_flag == 1)
            {
                terminal_send_data.send_power_off_result_flag = 2; //反馈控制结果到客户端
            } 
            break;
        } 
        case 0x01 :
        {
            ble_send_data.ble_send_user_cmd_state = CMD_FAILED;
            if(terminal_send_data.send_power_off_result_flag == 1)
            {
                terminal_send_data.send_power_off_result_flag = 3;
            } 
            break;
        } 
        default : break;
    }
    ble_send_data.ble_send_cmd_num = BLE_CMD_MIN;
}


static void ble_user_power_on_respond(void)
{
    switch(receive_buff[7])
    {
        case 0x00 : 
        {
            ble_send_data.ble_send_user_cmd_state = CMD_SUCCEED;
            if(terminal_send_data.send_power_on_result_flag == 1)
            {
                terminal_send_data.send_power_on_result_flag = 2; //反馈控制结果到客户端
            } 
            break;
        }
        case 0x01 :
        {
            ble_send_data.ble_send_user_cmd_state = CMD_FAILED;
            if(terminal_send_data.send_power_on_result_flag == 1)
            {
                terminal_send_data.send_power_on_result_flag = 3; //反馈控制结果到客户端
            } 
            break;
        } 
        default : break;
    }
    ble_send_data.ble_send_cmd_num = BLE_CMD_MIN;
}

static void ble_user_relay_state(void)
{
    for(uint8_t i=0; i<ble_receive_data.ble_num; i++)//获取id，存入对应的设备的继电器状态变量中
    {
        if(receive_buff[2] == ble_receive_data.ble_slave_id_data[i].ble_slave_id[0])
        {
            if(receive_buff[3] == ble_receive_data.ble_slave_id_data[i].ble_slave_id[1])
            {
                ble_receive_data.ble_slave_id_data[i].ble_slave_relay_state_last = ble_receive_data.ble_slave_id_data[i].ble_slave_relay_state;
                ble_receive_data.ble_slave_id_data[i].ble_slave_relay_state = receive_buff[7];
                ble_receive_data.ble_slave_id_data[i].ble_slave_state = ON_LINE;//连接状态更新为正常连接。
                ble_receive_data.ble_slave_id_data[i].ble_slave_time = 0;//清除计时数，开始离线计时
                terminal_send_data.send_relay_state_flag = 1;
                break;//已存入正常连接的设备的继电器状态变量中
            }
        }
    }
}

static void ble_user_timing_off_respond(void)
{
    switch(receive_buff[7])
    {
        case 0x00 :
        {
            ble_send_data.ble_send_user_cmd_state = CMD_SUCCEED;
            if(terminal_send_data.send_timing_off_result_flag == 1)
            {
                terminal_send_data.send_timing_off_result_flag = 2; //反馈控制结果到客户端
            } 
            break;
        } 
        case 0x01 :
        {
            ble_send_data.ble_send_user_cmd_state = CMD_FAILED;
            if(terminal_send_data.send_timing_off_result_flag == 1)
            {
                terminal_send_data.send_timing_off_result_flag = 3;
            } 
            break;
        } 
        default : break;
    }
    ble_send_data.ble_send_cmd_num = BLE_CMD_MIN;
}

static void ble_user_timing_on_respond(void)
{
    switch(receive_buff[7])
    {
        case 0x00 :
        {
            ble_send_data.ble_send_user_cmd_state = CMD_SUCCEED;
            if(terminal_send_data.send_timing_on_result_flag == 1)
            {
                terminal_send_data.send_timing_on_result_flag = 2; //反馈控制结果到客户端
            } 
            break;
        } 
        case 0x01 :
        {
            ble_send_data.ble_send_user_cmd_state = CMD_FAILED;
            if(terminal_send_data.send_timing_on_result_flag == 1)
            {
                terminal_send_data.send_timing_on_result_flag = 3;
            } 
            break;
        } 
        default : break;
    }
    ble_send_data.ble_send_cmd_num = BLE_CMD_MIN;
}

static void ble_user_time_off_respond(void)
{
    switch(receive_buff[7])
    {
        case 0x00 :
        {
            ble_send_data.ble_send_user_cmd_state = CMD_SUCCEED;
            if(terminal_send_data.send_time_off_result_flag == 1)
            {
                terminal_send_data.send_time_off_result_flag = 2; //反馈控制结果到客户端
            } 
            break;
        } 
        case 0x01 :
        {
            ble_send_data.ble_send_user_cmd_state = CMD_FAILED;
            if(terminal_send_data.send_time_off_result_flag == 1)
            {
                terminal_send_data.send_time_off_result_flag = 3;
            } 
            break;
        } 
        default : break;
    }
    ble_send_data.ble_send_cmd_num = BLE_CMD_MIN;
}

static void ble_user_time_on_respond(void)
{
    switch(receive_buff[7])
    {
        case 0x00 :
        {
            ble_send_data.ble_send_user_cmd_state = CMD_SUCCEED;
            if(terminal_send_data.send_time_on_result_flag == 1)
            {
                terminal_send_data.send_time_on_result_flag = 2; //反馈控制结果到客户端
            } 
            break;
        } 
        case 0x01 :
        {
            ble_send_data.ble_send_user_cmd_state = CMD_FAILED;
            if(terminal_send_data.send_time_on_result_flag == 1)
            {
                terminal_send_data.send_time_on_result_flag = 3;
            } 
            break;
        } 
        default : break;
    }
    ble_send_data.ble_send_cmd_num = BLE_CMD_MIN;
}